//用pinia管理三级导航数据

import { defineStore } from 'pinia'

//引入接口函数
import {reqGetCategory1ListData,reqGetCategory2ListData,reqGetCategory3ListData}from '@/api/product/category'

import type { CategoryStoreModel } from '@/api/product/categoryModel'
// 模块对象
export const useCategoryListStore = defineStore('categoryList',{
  state:(): CategoryStoreModel=>{
    return{
      //用于收集用户选择的真正id
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,
      // 用于存储服务器响应回来的分类数据
      category1List:[],
      category2List:[],
      category3List:[],
    }
  },
  actions:{
    //获取一级分类数据
    async getCategory1ListData(){
      const res = await reqGetCategory1ListData()
      // console.log('qqq',res);
      this.category1List = res
    },
    // 获取二级分类数据
    async getCategory2ListData(id: number){
      //保存1Id
      this.category1Id = id

      //如果一级导航变了，二三级导航清空重置
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []
      
      
      // 调用接口函数发送请求获取二级数据
      const res = await reqGetCategory2ListData(id)
     
      
      //存储数据
      this.category2List = res
    
    },
    // 获取三级分类数据
    async getCategory3ListData(id: number){
      this.category2Id = id

      //重置id
      this.category3Id = undefined
      this.category3List = await reqGetCategory3ListData(id)
    },
    //设置三级分类ID
    setCategory3Id(id:number){
      this.category3Id = id
    }
  },
  getters:{},

})